package com.yzf.demo.demo.Thread.lock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Created by 于占峰 on 2020/4/4/004.
 */
public class CountDownLatchDemo1 implements Runnable {

    static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) {
        //模拟1000个线程并发访问
        for (int i = 0; i < 1000; i++) {
            new Thread(new CountDownLatchDemo1()).start();
        }
        System.out.println("唤醒所有线程:-----------------> countDownLatch.countDown()");
        countDownLatch.countDown();//唤醒所有阻塞的线程
    }

    @Override
    public void run() {
        try {
            //让每一个线程都阻塞
            System.out.println("阻塞当前线程:" + Thread.currentThread().getName());
            countDownLatch.await();
            //countDownLatch.await(10L, TimeUnit.SECONDS); 避免所有线程卡在阻塞状态不懂 可以传入超时时间
            System.out.println("当前线程被唤醒:" + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
